/**
 * 
 */
package org.mspring.crm.client.frame;

import java.util.List;

import org.mspring.crm.client.model.ItemModel;
import org.mspring.crm.client.remote.ItemRemote;

import com.extjs.gxt.ui.client.Style.HideMode;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.TabPanel.TabPosition;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalSplitPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Gao Youbo
 * @since 2012-3-12
 * 
 */
public class SplitFrameCreator extends AbstractFrameCreator {

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mspring.crm.client.widget.frame.AbstractFrameCreator#createFrame(
     * org.mspring.crm.client.model.ItemModel[])
     */
    @Override
    public Widget createFrame(ItemModel... items) {
        // TODO Auto-generated method stub
        final VerticalSplitPanel verticalSplitPanel = new VerticalSplitPanel();
        if (items != null && items.length > 0) {
            for (int i = 0; i < items.length; i++) {
                ItemModel item = items[i];
                ItemRemote.Util.getInstance().findTabItem(item.getId(), new AsyncCallback<List<ItemModel>>() {

                    @Override
                    public void onSuccess(List<ItemModel> result) {
                        // TODO Auto-generated method stub
                        if (result != null && result.size() > 0) {
                            if (result.size() == 1) {
                                Panel panel = new SimplePanel();
                                panel.add(new HtmlContainer(result.get(0).getName()));
                                verticalSplitPanel.add(panel);
                            }
                            else {
                                TabPanel tabPanel = new TabPanel();
                                //tabPanel.setTabPosition(TabPosition.BOTTOM);
                                tabPanel.setBorderStyle(false);
                                tabPanel.setBodyBorder(false);
                                
                                for (ItemModel item : result) {
                                    TabItem tabItem = new TabItem();
                                    tabItem.setScrollMode(Scroll.AUTO);
                                    // if (entry.isFill()) {
                                    tabItem.setLayout(new FitLayout());
                                    tabItem.setScrollMode(Scroll.NONE);
                                    // }

                                    // tabItem.setHideMode(entry.getHideMode());
                                    tabItem.setHideMode(HideMode.DISPLAY);
                                    tabItem.setText(item.getName());
                                    
                                    tabPanel.add(tabItem);
                                    if (item.getSelected()) {
                                        tabPanel.setSelection(tabItem);
                                    }
                                }
                                verticalSplitPanel.add(tabPanel);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub

                    }
                });
                // panel.add(new HtmlContainer(items[i].getName()));
            }
        }
        return verticalSplitPanel;
    }

}
